package dhm.com.hpx.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.AllSourceAdapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.GetAllSourceBean;
import dhm.com.hpx.utils.SpUtils;

public class AllsourceActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.recy)
    XRecyclerView recy;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private PressenterImpl pressenter;
    private int page=1;
    private AllSourceAdapter adapter;
    private List<GetAllSourceBean.DataBean> sourceData;

    private String userType;
    @Override
    protected int getLayout() {
        return R.layout.activity_allsource;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        page = 1;
//设置 Header 为 贝塞尔雷达 样式
//        refreshLayout.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true));
//设置 Footer 为 球脉冲 样式
//        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
    }

    @Override
    protected void initData() {
        userType = SpUtils.getString(this, "userType");
        GridLayoutManager manager=new GridLayoutManager(this,4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup(){

            @Override
            public int getSpanSize(int i) {
                return 4;
            }
        });

        recy.setFootViewText("正在加载。。。","暂无更多");
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy.setLayoutManager(manager);
//        recy.setPullRefreshEnabled(true);
//        recy.setLoadingMoreEnabled(true);


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page=1;
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", page+"");
                pressenter.sendMessage(AllsourceActivity.this,Constant.customer_list,map, GetAllSourceBean.class);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", page+"");
                pressenter.sendMessage(AllsourceActivity.this,Constant.customer_list,map, GetAllSourceBean.class);
            }
        });

/*
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", page+"");
                pressenter.sendMessage(AllsourceActivity.this,Constant.customer_list,map, GetAllSourceBean.class);
            }

            @Override
            public void onLoadMore() {
                Map<String,String> map=new HashMap<>();
                map.put("token", Constant.TOKEN);
                map.put("page", page+"");
                pressenter.sendMessage(AllsourceActivity.this,Constant.customer_list,map, GetAllSourceBean.class);
            }
        });
*/

        Map<String,String> map=new HashMap<>();
        map.put("token", Constant.TOKEN);
        map.put("page", page+"");
        pressenter.sendMessage(this,Constant.customer_list,map, GetAllSourceBean.class);
    }

    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof GetAllSourceBean){
            GetAllSourceBean getAllSourceBean= (GetAllSourceBean) data;
            if (getAllSourceBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    sourceData = getAllSourceBean.getData();
                    adapter = new AllSourceAdapter(this);
                    adapter.setdeleteClick(new AllSourceAdapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            switch (userType) {
                                case "0":
                                    Toast.makeText(AllsourceActivity.this, "暂无权限查看，请升级会员", Toast.LENGTH_SHORT).show();
                                    break;
                                case "4":
                                    Toast.makeText(AllsourceActivity.this, "您的会员已过期", Toast.LENGTH_SHORT).show();
                                    break;
                                case "1":
                                case "2":
                                case "3":
                                case "5":
                                default:
                                    Intent intent=new Intent(AllsourceActivity.this,FollowActivity.class);
                                    intent.putExtra("url",sourceData.get(posttion).getCustomerUrl());
                                    startActivity(intent);
                            }
                        }
                    });
                    recy.setAdapter(adapter);
                }else {
                    sourceData.addAll(getAllSourceBean.getData());
                }
                adapter.setList(sourceData);
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(this, getAllSourceBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
//            recy.loadMoreComplate();
//            recy.refreshComplate();
//            recy.loadMoreComplete();
//            recy.refreshComplete();
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void fail(String error) {

    }
}
